<?php

namespace App\Admin\Controllers;

use App\Models\User;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class UserController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('用户列表')
            ->description('用户列表')
            ->body($this->grid());
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑用户')
            ->description('编辑用户')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('创建用户')
            ->description('创建用户')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);

        $grid->id('Id');
        $grid->avatar('头像')->display(function ($avatar) {
            return '<img src="' . $avatar . '" width="50px"/>';
        });
        $grid->name('用户名称');
        $grid->email('邮箱');
        $grid->email_verified_at('邮箱认证');
        $grid->introduction('用户介绍');
        $grid->created_at('注册日期');

        $grid->actions(function ($actions) {
            // 不在每一行后面展示查看按钮
            $actions->disableView();
            $actions->disableEdit();
        });
        $grid->disableCreateButton();

        $grid->filter(function ($filter) {

            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->column(1 / 2, function ($filter) {
                $filter->like('name', '用户名称');
                $filter->where(function ($query) {
                    switch ($this->input) {
                        case 'yes':
                            $query->whereNotNull('email_verified_at');
                            break;
                        case 'no':
                            $query->whereNull('email_verified_at');
                            break;
                    }
                }, '邮箱认证', 'email_verified_at')->radio([
                    'yes' => '已认证',
                    'no'  => '未认证',
                ]);
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->like('email', '邮箱');
            });

        });

        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);

        $form->text('name', '用户名称');
        $form->email('email', '邮箱地址');
        $form->datetime('email_verified_at', '邮箱认证')->default(date('Y-m-d H:i:s'));
        $form->image('avatar', '头像');
        $form->text('introduction', '用户简介');

        return $form;
    }
}
